package tests.org.nicta.colt;

import static org.junit.Assert.*;

import org.junit.Test;
import org.nicta.colt.utils.ColtUtils;

import cern.colt.matrix.DoubleFactory1D;
import cern.colt.matrix.DoubleFactory2D;
import cern.colt.matrix.DoubleMatrix1D;
import cern.colt.matrix.DoubleMatrix2D;
import cern.colt.matrix.doublealgo.Transform;
import cern.colt.matrix.linalg.Algebra;
import cern.jet.math.Functions;

public class coltTests {

	// Test cases for ColtUtils 
	// As Colt documentation is not very good this is created to play
	// around with colt library
	@Test
	public void test() {
		double [] u = {1,2,3};
		double [] v = {4,5,6};
		
		double [] a = {1,3,3};
		double [] b = {1,0,6};

		Algebra algebra = new Algebra();
		DoubleMatrix1D x1 = DoubleFactory1D.sparse.make(u);
		DoubleMatrix1D x2 = DoubleFactory1D.sparse.make(v);
		DoubleMatrix1D x3 = DoubleFactory1D.sparse.make(a);
		DoubleMatrix1D x4 = DoubleFactory1D.sparse.make(b);
		DoubleMatrix2D M = DoubleFactory2D.sparse.make(3,3);
		DoubleMatrix2D N = DoubleFactory2D.sparse.make(3,3);

		algebra.multOuter(x1, x2, M);
		algebra.multOuter(x3, x4, N);
		
		System.out.println(M);
		System.out.println(N);
		System.out.println(algebra.normF(M));

		System.out.println(DoubleFactory2D.dense.appendColumns(M, N));
		
		ColtUtils.sumColumnByVector(M,x1,0);
		
		System.out.println(M);

		double[] result = new double[18];
		ColtUtils.mergeFlattenTo1D(M,N, result);
		M.assign(Functions.mult(0));
		N.assign(Functions.mult(0));
		System.out.println(M);
		System.out.println(N);
		for(int i=0;i<18;i++)
			System.out.print(result[i]+"\t");
		ColtUtils.assignByUnflattening(result, M, N);
		System.out.println(M);
		System.out.println(N);
//		N = ColtUtils.multScalar(M, -2);
//		System.out.println(N);
//		System.out.println(M);
//
//		M.assign(M,Functions.minus);
//		System.out.println(M);
//
//		
//		double[] proj = new double[] {0.12, 0.55,0.63,0.98, 0.36};
//		
//		double[] uf = new double[1700];
//		uf[55] = 1.0;
//		uf[1000] = 2;
//		
//		DoubleMatrix1D projection = DoubleFactory1D.sparse.make(proj);
//
//
//		DoubleMatrix1D userFeature = DoubleFactory1D.sparse.make(uf);
//		
//		DoubleMatrix1D x = DoubleFactory1D.sparse.make(uf);
//
//		for(int i=0;i<100000;i++){
//			DoubleMatrix2D movieGradient = DoubleFactory2D.sparse.make(5,
//					1700);
//			algebra.multOuter(projection,userFeature,movieGradient);
//
//			//
//			//DoubleMatrix1D userFeature = x.copy();
//
//		}
//		
//		long start = System.currentTimeMillis();
//		for(int j=0;j<10000000;j++){
//			DoubleMatrix2D M1 = DoubleFactory2D.sparse.make(10,2000);
//		}
//		long end = System.currentTimeMillis();
//        System.out.println("Time: " + (end-start) + "ms");
//
//		DoubleMatrix2D M2 = DoubleFactory2D.sparse.random(10,2000);
//		DoubleMatrix2D M2_temp = DoubleFactory2D.sparse.make(10,2000);
//
//		start = System.currentTimeMillis();
//		for(int j=0;j<10000000;j++){
//			M2_temp.assign(M2);
//		}
//		end = System.currentTimeMillis();
//        System.out.println("Time: " + (end-start) + "ms");
//		
	}

}
